﻿package mortal.game.view.mainUI.shortcutbar
{
    import Message.DB.Tables.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.display.*;
    import mortal.common.global.*;
    import mortal.common.shortcutsKey.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.configBase.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.mainUI.shortcutbar.panel.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class ShortcutBar extends ShortcutBarBase
    {
        private var _items:Array;
        private var _itemSize:int = 36;
        private var _vGap:int = 2;
        private var _hGap:int = 4;
        private var _colNum:int = 10;
        private var _line1Container:Sprite;
        private var _line2Container:Sprite;
        private var _angerBarPanel:AngerBarPanel;
        private var _expSprite:Sprite;
        private var _expbg:ScaleBitmap;
        private var _expBar:BaseProgressBar;
        private var _expTxt:TextField;
        private var _btnUp:GLoadedButton;
        private var _btnDown:GLoadedButton;
        private var _btnLock:GLoadedButton;
        private var _btnUnlock:GLoadedButton;
        private var _bmpFlower:GBitmap;
        private var _shortcutBarBg:GBitmap;
        private var _dragStatus:int = 0;
        private var _isFirst:Boolean = true;

        public function ShortcutBar()
        {
            this.addEventListener(DragEvent.Event_Move_In, this.moveInHandler);
            this.addEventListener(DragEvent.Event_Throw_goods, this.throwHandler);
            this.layer = LayerManager.uiLayer;
            this.mouseEnabled = false;
            return;
        }// end function

        private function moveInHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragItem as CDItem;
            var _loc_3:* = event.dropItem as CDItem;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarMoveIn, {from:_loc_2, to:_loc_3}));
            return;
        }// end function

        private function throwHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragItem as ShortcutBarItem;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarThrow, _loc_2));
            return;
        }// end function

        public function getDragSource(param1:int) : Object
        {
            var _loc_2:* = this._items[param1] as ShortcutBarItem;
            if (_loc_2 == null)
            {
                return null;
            }
            return _loc_2.dragSource;
        }// end function

        public function getAllItems() : Array
        {
            return this._items;
        }// end function

        protected function createItems() : void
        {
            var _loc_1:ShortcutBarItem = null;
            var _loc_2:SkillInfo = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            this._items = [];
            this._line1Container = new Sprite();
            this._line1Container.x = -10;
            this._line1Container.y = 1;
            this.addChild(this._line1Container);
            _loc_3 = 11;
            while (_loc_3 <= 19)
            {
                
                _loc_1 = new ShortcutBarItem(_loc_3);
                _loc_1.x = (_loc_3 - 11) * 42 + 23;
                _loc_1.y = 0;
                _loc_1.setSize(42, 42);
                _loc_1.isDragAble = true;
                _loc_1.isDropAble = true;
                _loc_1.isThrowAble = true;
                this._line1Container.addChild(_loc_1);
                this._items[_loc_3] = _loc_1;
                _loc_1.bgName = ImagesConst.shortcutItemBg;
                _loc_3++;
            }
            this._line2Container = new Sprite();
            this.addChild(this._line2Container);
            this._line2Container.x = -10;
            this._line2Container.y = 45;
            _loc_3 = 1;
            while (_loc_3 <= 10)
            {
                
                _loc_1 = new ShortcutBarItem(_loc_3);
                _loc_1.x = (_loc_3 - 1) * 42.5;
                if (_loc_3 == 1)
                {
                    _loc_1.x = 1;
                }
                _loc_1.y = 1;
                _loc_1.setSize(42, 42);
                _loc_1.isDragAble = true;
                _loc_1.isDropAble = true;
                _loc_1.isThrowAble = true;
                this._line2Container.addChild(_loc_1);
                this._items[_loc_3] = _loc_1;
                _loc_1.bgName = ImagesConst.shortcutItemBg2;
                _loc_3++;
            }
            this._angerBarPanel = new AngerBarPanel();
            this.addChild(this._angerBarPanel);
            this._angerBarPanel.x = this._line2Container.x - 94;
            this._angerBarPanel.y = 19;
            this._expSprite = new Sprite();
            this._expSprite.addEventListener(MouseEvent.ROLL_OVER, this.showNum);
            this._expSprite.addEventListener(MouseEvent.ROLL_OUT, this.hideNum);
            this.addChild(this._expSprite);
            this._expbg = UIFactory.bg(-135, this._line2Container.y + this._line2Container.height + 3, 1024, 15, this._expSprite, ImagesConst.FunctionBar);
            this._shortcutBarBg = UIFactory.gBitmap(ImagesConst.FunctionBarBg, -118, this._line2Container.y + this._line2Container.height - 60);
            this.addChildAt(this._shortcutBarBg, 0);
            this._expBar = new BaseProgressBar();
            this._expBar.createDisposedChildren();
            this._expBar.setLabel(BaseProgressBar.ProgressBarTextNone);
            this._expBar.setProgress(BitmapDataConst.AlphaBMD, true, 2, 4, 890, 13);
            this._expBar.setTweenProgress(ImagesConst.ExpBar2, true, 2, 4, 890, 13);
            this._expBar.x = -68;
            this._expBar.y = this._line2Container.y + this._line2Container.height + 2;
            this._expSprite.addChild(this._expBar);
            while (_loc_4 < 9)
            {
                
                UIFactory.gBitmap(ImagesConst.ExpSpriteLine, _loc_4 * 89 + 21.4, 94, this._expSprite);
                _loc_4++;
            }
            var _loc_5:* = GlobalStyle.textFormatBai;
            this._expTxt = new TextField();
            this._expTxt.width = 520;
            this._expTxt.y = this._line2Container.y + this._line2Container.height + 1;
            this._expTxt.selectable = false;
            this._expTxt.defaultTextFormat = _loc_5;
            this.updateExp(Cache.instance.role.roleInfo.experience);
            return;
        }// end function

        private function showNum(event:MouseEvent = null) : void
        {
            this._expSprite.addChild(this._expTxt);
            this.updateExpTxt();
            return;
        }// end function

        private function hideNum(event:MouseEvent) : void
        {
            if (this._expTxt.parent)
            {
                this._expTxt.parent.removeChild(this._expTxt);
            }
            return;
        }// end function

        public function updateExpTxt() : void
        {
            var _loc_1:String = null;
            var _loc_2:int = 0;
            if (this._expTxt.parent)
            {
                _loc_1 = GameUtil.getExpAdditionStr();
                _loc_2 = Cache.instance.publicCache.worldLevel;
                this._expTxt.htmlText = this._expBar.value + "/" + this._expBar.totalValue + "(" + int(this._expBar.value / this._expBar.totalValue * 100) + "%)" + Language.getStringByParam(35386, _loc_2) + _loc_1;
                this._expTxt.x = this._expbg.x + (this._expbg.width - this._expTxt.textWidth) * 0.5;
            }
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            this.createItems();
            var _loc_1:* = UIFactory.gBitmap(ImagesConst.shortcutBarLockBg, this._line2Container.x + 424, this._line2Container.y + 2, this);
            pushUIToDisposeVec(_loc_1);
            this._btnUp = UIFactory.gLoadedButton(ImagesConst.shortcutBarUp_upSkin, _loc_1.x + 4, _loc_1.y + 2, 11, 10, this);
            this.setBtnEnable(this._btnUp, false);
            this._btnDown = UIFactory.gLoadedButton(ImagesConst.shortcutBarDown_upSkin, this._btnUp.x, this._btnUp.y + 23, 11, 10, this);
            this._btnUnlock = UIFactory.gLoadedButton(ImagesConst.ShortcutBarUnLock_upSkin, this._line2Container.x - 26, this._line2Container.y + 6, 24, 24, this);
            this._btnLock = UIFactory.gLoadedButton(ImagesConst.shortcutBarLock_upSkin, this._btnUnlock.x, this._btnUnlock.y, 24, 24, this);
            this._bmpFlower = UIFactory.gBitmap(ImagesConst.Shortcut_Flower, this._line2Container.x + 386, this._line2Container.y - 14, this);
            this.addChildAt(this._bmpFlower, 0);
            this._btnUp.configEventListener(MouseEvent.CLICK, this.clickUpHandler);
            this._btnDown.configEventListener(MouseEvent.CLICK, this.clickDownHandler);
            this._btnLock.configEventListener(MouseEvent.CLICK, this.clickLockHandler);
            this._btnUnlock.configEventListener(MouseEvent.CLICK, this.clickUnlockHandler);
            return;
        }// end function

        public function isTopLineShowing() : Boolean
        {
            return this._line1Container.parent != null;
        }// end function

        public function updateItemsShow(param1:Boolean, param2:Boolean = false) : void
        {
            var _loc_3:ShortcutBarItem = null;
            var _loc_4:DisplayObjectContainer = null;
            if (!param1)
            {
                if (param2 && this._dragStatus == 1)
                {
                    this.clickDownHandler(null);
                    this._dragStatus = 0;
                }
                for each (_loc_3 in this._items)
                {
                    
                    if (_loc_3.dragSource == null && _loc_3.pos >= 11)
                    {
                        DisplayUtil.removeMe(_loc_3);
                    }
                }
            }
            else
            {
                if (param2)
                {
                    if (this._line1Container.parent == null)
                    {
                        this.clickUpHandler(null);
                        this._dragStatus = 1;
                    }
                }
                for each (_loc_3 in this._items)
                {
                    
                    _loc_4 = this._line2Container;
                    if (_loc_3.pos >= 11)
                    {
                        _loc_4 = this._line1Container;
                    }
                    if (_loc_3.parent == null)
                    {
                        _loc_4.addChild(_loc_3);
                    }
                }
            }
            return;
        }// end function

        public function showItem(param1:int) : void
        {
            var _loc_2:* = this._items[param1] as ShortcutBarItem;
            var _loc_3:* = this._line2Container;
            if (_loc_2.pos >= 11)
            {
                _loc_3 = this._line1Container;
            }
            if (_loc_2.parent == null)
            {
                _loc_3.addChild(_loc_2);
            }
            return;
        }// end function

        public function updateLockStatus(param1:Boolean) : void
        {
            var _loc_3:ShortcutBarItem = null;
            if (param1)
            {
                DisplayUtil.removeMe(this._btnLock);
                this.addChild(this._btnUnlock);
            }
            else
            {
                DisplayUtil.removeMe(this._btnUnlock);
                this.addChild(this._btnLock);
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._items.length)
            {
                
                _loc_3 = this._items[_loc_2];
                if (_loc_3 == null)
                {
                }
                else
                {
                    _loc_3.setLock(param1);
                }
                _loc_2++;
            }
            return;
        }// end function

        private function clickLockHandler(event:MouseEvent) : void
        {
            DisplayUtil.removeMe(this._btnLock);
            this.addChild(this._btnUnlock);
            SystemSetting.instance.isLockShortcut.value = 1;
            SystemSetting.instance.save();
            return;
        }// end function

        private function clickUnlockHandler(event:MouseEvent) : void
        {
            DisplayUtil.removeMe(this._btnUnlock);
            this.addChild(this._btnLock);
            SystemSetting.instance.isLockShortcut.value = 0;
            SystemSetting.instance.save();
            return;
        }// end function

        private function clickUpHandler(event:MouseEvent) : void
        {
            this.setBtnEnable(this._btnUp, false);
            this.setBtnEnable(this._btnDown, true);
            this.addChild(this._line1Container);
            return;
        }// end function

        private function setBtnEnable(param1:GButton, param2:Boolean) : void
        {
            param1.mouseChildren = param2;
            param1.mouseEnabled = param2;
            if (param2)
            {
                param1.filters = [];
            }
            else
            {
                param1.filters = [FilterConst.colorFilter];
            }
            return;
        }// end function

        private function clickDownHandler(event:MouseEvent) : void
        {
            this.setBtnEnable(this._btnUp, true);
            this.setBtnEnable(this._btnDown, false);
            DisplayUtil.removeMe(this._line1Container);
            return;
        }// end function

        public function updateExp(param1:int = 0) : void
        {
            var tExperience:TExperience;
            var setValue:Function;
            var point:Point;
            var value:* = param1;
            setValue = function () : void
            {
                _expBar.setValue(value, tExperience.upgradeNeedExperience, true);
                updateExpTxt();
                return;
            }// end function
            ;
            var level:* = Cache.instance.role.roleInfo.level;
            tExperience = ConfigCenter.getConfigs(ConfigConst.expLevel, "level", level, true) as TExperience;
            if (!this._isFirst)
            {
                point = this._expBar.progressBar.localToGlobal(new Point(0, 0));
                point.x = point.x + (this._expBar.progressBar.width - 12);
                GuideEffect.playAddExpEffect(value - this._expBar.value, point.x, point.y - 15, setValue, -1, -1, GuideFlyEffect.color_Green, true);
            }
            else
            {
                this.setValue();
            }
            this._isFirst = false;
            return;
        }// end function

        public function updateExpBgLevel() : void
        {
            var tExperience:TExperience;
            var setValue:Function;
            setValue = function () : void
            {
                _expBar.totalValue = tExperience.upgradeNeedExperience;
                updateExpTxt();
                return;
            }// end function
            ;
            tExperience = ConfigCenter.getConfigs(ConfigConst.expLevel, "level", Cache.instance.role.roleInfo.level, true) as TExperience;
            var point:* = this._expBar.progressBar.localToGlobal(new Point(0, 0));
            point.x = point.x + (this._expBar.progressBar.width - 12);
            GuideEffect.playAddExpEffect(tExperience.upgradeNeedExperience - this._expBar.value, point.x, point.y - 15, setValue, -1, -1, GuideFlyEffect.color_Green);
            return;
        }// end function

        override public function get width() : Number
        {
            if (this._line2Container == null)
            {
                return 1;
            }
            return this._line2Container.width;
        }// end function

        override public function get height() : Number
        {
            if (this._line2Container == null)
            {
                return 1;
            }
            return this._expbg.y + this._expbg.height;
        }// end function

        public function updateLabelByIndex(param1:int, param2:String) : void
        {
            var _loc_3:* = this._items[param1];
            if (_loc_3)
            {
                _loc_3.setLabelTxt(param2);
            }
            return;
        }// end function

        public function updateShortcutKey(param1:Array) : void
        {
            var _loc_2:KeyMapData = null;
            var _loc_3:ShortcutBarItem = null;
            var _loc_4:String = null;
            var _loc_5:int = 0;
            while (_loc_5 < param1.length)
            {
                
                _loc_2 = param1[_loc_5];
                if (_loc_2.key is int)
                {
                    _loc_3 = this._items[_loc_2.key];
                    if (_loc_3)
                    {
                        if (_loc_2.keyData)
                        {
                            _loc_4 = _loc_2.keyData.shortcutsName;
                        }
                        else
                        {
                            _loc_4 = "";
                        }
                        _loc_3.setLabelTxt(_loc_4);
                    }
                }
                _loc_5++;
            }
            return;
        }// end function

        public function updateShortcutSource(param1:int, param2:Object) : void
        {
            var _loc_3:* = this._items[param1];
            if (_loc_3 != null)
            {
                _loc_3.dragSource = param2;
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            this.onStageResize();
            LayerManager.uiLayer.addChildAt(this, (LayerManager.uiLayer.numChildren - 1));
            return;
        }// end function

        public function onStageResize() : void
        {
            if (Global.stage.stageWidth >= 1366)
            {
                this.x = Global.stage.stageWidth - this.width >> 1;
            }
            else
            {
                this.x = Global.stage.stageWidth - this._expSprite.width - this._angerBarPanel.x + 27;
            }
            this.y = Global.stage.stageHeight - this.height;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarResize));
            return;
        }// end function

        override public function hide() : void
        {
            DisplayUtil.removeMe(this);
            return;
        }// end function

        public function updateAnger() : void
        {
            this._angerBarPanel.updateAnger();
            return;
        }// end function

        public function updateSelected() : void
        {
            this._angerBarPanel.updateSelected();
            return;
        }// end function

        public function get xpBtn() : GLoadedButton
        {
            return this._angerBarPanel.xpBtn;
        }// end function

        public function thawFrzzing() : void
        {
            this._angerBarPanel.thawFrzzing();
            return;
        }// end function

    }
}
